//
//  ConsultaionTableViewCell.swift
//  OnceNectar
//
//  Created by 法正磊 on 2018/1/30.
//  Copyright © 2018年 com.inwhoop. All rights reserved.
//

import UIKit
import Alamofire
import SwiftyJSON
import PKHUD
protocol ConsultaionTableViewCellDelegate: NSObjectProtocol {
    func delCell(model:MyContentModel, cell:ConsultaionTableViewCell) -> Void
}
typealias deleteBlock = () -> Void
class ConsultaionTableViewCell: UITableViewCell, BottomShadow, AddCornerUIRectCorner {

    @IBOutlet weak var minePLabel: UIButton!
    @IBOutlet weak var browsingVolumeLabel: UIButton!
    @IBOutlet weak var bottomView: UIView!
    @IBOutlet weak var introductionLabel: UILabel!
    @IBOutlet weak var levelAndConstellationLabel: UILabel!
    @IBOutlet weak var userNameLabel: UILabel!
    @IBOutlet weak var headerImageView: UIImageView!
    @IBOutlet weak var showImageView: UIImageView!
    @IBOutlet weak var mainView: UIView!
    @IBOutlet weak var imageViewHeight: NSLayoutConstraint!
    @IBOutlet weak var likeBtn: UIButton!
    @IBOutlet weak var commentBtn: UIButton!
    
    @IBOutlet weak var statusLabel: UILabel!
    weak var delegate: ConsultaionTableViewCellDelegate?
    
    var deleteblock: deleteBlock?
    var model: MyContentModel?
    override func awakeFromNib() {
        super.awakeFromNib()
        imageViewHeight.constant = kHeight(r: imageViewHeight.constant)
        setShadow(view: mainView, opacity: bottomShadowOpacity, size: bottomShadowOffset, color: UIColor.black, cornerradius: CORNERRADIUS)
        addCornerWithView(aView: showImageView, aCorners: [.topLeft, .topRight], aSize: CGSize(width: CORNERRADIUS, height: CORNERRADIUS))
        addCornerWithView(aView: bottomView, aCorners: [.bottomLeft, .bottomRight], aSize: CGSize(width: CORNERRADIUS, height: CORNERRADIUS))
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
    }
    
    func showVideoData(model: VideoModel) {

        let path:String = "\(BaseURL)\(model.coverImgPath!)"
        let url = URL.init(string: path.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)!)
        if path.hasSuffix("gif") {
            showImageView.setGifFromURL(url)
        }else {
            showImageView.sd_setImage(with: url! as URL, completed: nil)
        }

        if let name = model.channel?.name{
            self.minePLabel.setTitle(name, for: UIControlState.normal)
        }else {
            self.minePLabel.isHidden = true
        }
        if model.collected == "1" {
            self.likeBtn.isSelected = true
        }else {
            self.likeBtn.isSelected = false
        }
        let headUrl = appendUrl(suffix: (model.author?.headIcon)!)
        headerImageView.sd_setImage(with: headUrl as URL, completed: nil)
        self.likeBtn.setTitle(model.favorite, for: UIControlState.normal)
        self.commentBtn.setTitle(model.comment, for: UIControlState.normal)
        var titleStr:NSString = model.title as! NSString
        if titleStr.length > 20 {
            titleStr = titleStr.replacingCharacters(in: NSRange.init(location: 20, length: titleStr.length - 20), with: "...") as NSString
        }
        introductionLabel.text = titleStr as String
        userNameLabel.text = (model.author?.nickname!)!
        if model.author?.type == "internal" {
            levelAndConstellationLabel.text = ""
        }else {
            if let gradename = model.author?.grade?.gradeName!
            {
                levelAndConstellationLabel.text = gradename + (model.author?.grade?.gradeCode)!
            }
        }
       
        browsingVolumeLabel.setTitle(model.browse, for: UIControlState.normal)
    }
    
    func showContent1(model: MyContentModel, flag : Bool, showStatus: Bool) -> Void {
        self.model = model
        let path:String = "\(BaseURL)\(model.image!)"
        
        let url = URL.init(string: path.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)!)
        if Int(model.redirectType!)! > 1 {
            showImageView.setGifFromURL(url)
        }else {
            showImageView.sd_setImage(with: url! as URL, completed: nil)
        }
        if model.collected == "1" {
            self.likeBtn.isSelected = true
        }else {
            self.likeBtn.isSelected = false
        }
        self.likeBtn.setTitle(model.favorite, for: UIControlState.normal)
        self.commentBtn.setTitle(model.comment, for: UIControlState.normal)
        
        let headPath:String = "\(BaseURL)\((model.author?.headIcon!)!)"
        let headUrl = NSURL.init(string: headPath.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)!)
        headerImageView.sd_setImage(with: headUrl! as URL, completed: nil)
        userNameLabel.text = (model.author?.nickname!)!
        if model.author?.type == "internal" {
            levelAndConstellationLabel.text = ""
        }else {
            if let gradename = model.author?.grade?.gradeName!
            {
                levelAndConstellationLabel.text = gradename + (model.author?.grade?.gradeCode)!
            }
        }
        if let name = model.channel?.name{
            self.minePLabel.setTitle(name, for: UIControlState.normal)
        }else {
            self.minePLabel.isHidden = true
        }
        if model.statusDesc != nil && showStatus {
            statusLabel.text = "审核状态：" + model.statusDesc!
        }else {
            statusLabel.isHidden = true
        }
        browsingVolumeLabel.setTitle(model.browse, for: UIControlState.normal)
        var titleStr:NSString = model.title as! NSString
        if titleStr.length > 20 {
            titleStr = titleStr.replacingCharacters(in: NSRange.init(location: 20, length: titleStr.length - 20), with: "...") as NSString
        }
        introductionLabel.text = titleStr as String
        for ges in (self.contentView.gestureRecognizers)! {
            if ges.isKind(of: UILongPressGestureRecognizer.self){
                self.contentView.removeGestureRecognizer(ges)
            }
        }
        if flag {
            
            let long = UILongPressGestureRecognizer.init(target: self, action: #selector(handleLong(long:)))
            self.contentView.isUserInteractionEnabled = true
            self.contentView.addGestureRecognizer(long)
        }
    }
    
    func showConsultaionData(model: ArticleModel) {
        
        
        let path:String = "\(BaseURL)\(model.coverImgPath!)"
        
        
        let url = URL.init(string: path.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)!)
        if Int(model.redirectType!)! > 1 {
            showImageView.setGifFromURL(url)
        }else {
            showImageView.sd_setImage(with: url! as URL, completed: nil)
        }
        if model.collected == "1" {
            self.likeBtn.isSelected = true
        }else {
            self.likeBtn.isSelected = false
        }
        if let name1 = model.channel?.name {
            self.minePLabel.setTitle(name1, for: UIControlState.normal)
        }else {
            self.minePLabel.isHidden = true
        }
        
        let headPath:String = "\(BaseURL)\((model.author?.headIcon!)!)"
        let headUrl = NSURL.init(string: headPath.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)!)
        self.likeBtn.setTitle(model.favorite, for: UIControlState.normal)
        self.commentBtn.setTitle(model.comment, for: UIControlState.normal)
        var titleStr:NSString = model.title as! NSString
        if titleStr.length > 20 {
            titleStr = titleStr.replacingCharacters(in: NSRange.init(location: 20, length: titleStr.length - 20), with: "...") as NSString
        }
        introductionLabel.text = titleStr as String
        headerImageView.sd_setImage(with: headUrl! as URL, completed: nil)
        userNameLabel.text = (model.author?.nickname!)!

        if model.author?.type == "internal" {
            levelAndConstellationLabel.text = ""
        }else {
            if let gradename = model.author?.grade?.gradeName!
            {
                levelAndConstellationLabel.text = gradename + (model.author?.grade?.gradeCode)!
            }
        }
         browsingVolumeLabel.setTitle(model.browse, for: UIControlState.normal)
    
    }
    
    @IBAction func collectAction(_ sender: Any) {
        guard MineInfoModel.share.hasLoginIn() else {
            let loginVC = LoginViewController(nibName: "\(LoginViewController.self)", bundle: nil)
//            self.delegate?.navigationController?.pushViewController(loginVC, animated: true)
            return
        }
        
    }
    
    @objc func handleLong(long: UILongPressGestureRecognizer){
        
        if long.state == .began {
            self.delegate?.delCell(model:self.model!, cell:self)
        }
    }
    
    override func draw(_ rect: CGRect) {
       
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }
    
}
